package com.yjyw.tiexie.util;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;

/**
 * Created on 2019/7/26.
 *
 * @author ${AUTHOR}
 * TODO:
 */
public class NumberUtil {

    public static String money(String strFrom) {

        double number = 0;
        try {
            number = Double.valueOf(strFrom);
        } catch (Exception e) {
            number = 0;
        }
        if (StringUtil.isNullOrEmpty(strFrom)) {
            number = 0;
        }
        String str = "";
        BigDecimal bigDecimal = new BigDecimal(number).setScale(2, BigDecimal.ROUND_HALF_UP);
        str = bigDecimal.toString();
        return str;
    }

    public static String moneyNoZero(String strFrom) {

        double number = 0;

        try {
            number = Double.valueOf(strFrom);
        } catch (Exception e) {
            number = 0;
        }
        if (StringUtil.isNullOrEmpty(strFrom)) {
            number = 0;
        }
        String str = "";
        BigDecimal bigDecimal = new BigDecimal(number).setScale(2, BigDecimal.ROUND_HALF_UP);
        str = bigDecimal.toString();
        if (str.indexOf(".") > 0) {
            //正则表达
            str = str.replaceAll("0+?$", "");//去掉后面无用的零
            str = str.replaceAll("[.]$", "");//如小数点后面全是零则去掉小数点
        }
        return str;
    }


    public static String setMoney(String strFrom) {
        double number = 0;
        try {
            number = Double.valueOf(strFrom);
        } catch (Exception e) {
            number = 0;
        }
        String str = "";
        BigDecimal bigDecimal = new BigDecimal(number).setScale(2, BigDecimal.ROUND_HALF_UP);
        str = bigDecimal.toString();
        String[] strs = str.split("\\.");
        return strs[0];
    }


    public static String setMoney_(String strFrom) {
        double number = 0;
        try {
            number = Double.valueOf(strFrom);
        } catch (Exception e) {
            number = 0;
        }
        String str = "";
        BigDecimal bigDecimal = new BigDecimal(number).setScale(2, BigDecimal.ROUND_HALF_UP);
        str = bigDecimal.toString();
        String[] strs = str.split("\\.");
        return strs[1];
    }

    public static String moneyToWan(double str) {


        String monry = "";
        if (str > 100000) {//大于10万显示万
            DecimalFormat df = new DecimalFormat("######0");
            df.setRoundingMode(RoundingMode.DOWN);
            monry = df.format(Float.valueOf(str / 10000 + "")) + "万";
        } else {
            DecimalFormat df = new DecimalFormat("######0.00");
            df.setRoundingMode(RoundingMode.HALF_UP);
            monry = df.format(Float.valueOf(str + ""));
        }
        return monry;
    }

    public static String numToWan(double str) {

        String monry = "";
        if (str > 10000) {//大于1万显示万
            DecimalFormat df = new DecimalFormat("######0.00");
            df.setRoundingMode(RoundingMode.DOWN);
            monry = df.format(Float.valueOf(str / 10000 + "")) + "w";
        } else {
            DecimalFormat df = new DecimalFormat("######0");
            df.setRoundingMode(RoundingMode.HALF_UP);
            monry = df.format(Float.valueOf(str + ""));
        }
        return monry;
    }
}
